﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MovieModels;
using System.IO;
using System.Net;
using MKDataBase.Contracts;
using MK_Movie_Manager.EditMovieDeails;
using Manas.Core.Extensions;

namespace MK_Movie_Manager
{
    public partial class MovieDetailsComponent : UserControl
    {
        private IMDBMovie movieDetails;
        private int movieManagerID;
        private IDBProvider MovieDB;
        private MovieDetailsEditComponent editComponent;
        public MovieDetailsComponent(MovieDetailsEditComponent editComponent,EventHandler editAction)
        {
            this.MovieDB = MMSettings.CurrentDBInstance;
            this.editComponent = editComponent;
            InitializeComponent();
            this.btnEditDetails.Click += editAction;
        }

        public void UpDateMovieDetails(IMDBMovie movieDetails, int movieManagerID)
        {
            this.movieDetails = movieDetails;
            this.movieManagerID = movieManagerID;
            this.pbMoviePoster.Image = null;
            this.btnAddDetails.Enabled = true;
            InitializeMovie();
        }

        private void InitializeMovie()
        {
            this.lblMovieID.Text = this.movieDetails.MovieId;
            this.lblMovieTitle.Text = this.movieDetails.MovieTitle;
            this.lblIMDBUrl.Text = this.movieDetails.ImdbUrl;
            this.lblReleaseDate.Text = this.movieDetails.ReleaseDate;
            this.lblMovieRating.Text = this.movieDetails.Rating;
            this.lblTagLine.Text =  WebHelper.HtmlDecode(this.movieDetails.TagLine);
            this.rtbStoryline.Text = WebHelper.HtmlDecode(this.movieDetails.StoryLine);
            this.pbMoviePoster.ImageLocation = this.movieDetails.PosterUrl;
            this.lblGenre.Text = this.movieDetails.Genres.Aggregate((f, l) => f + '#' + l);           
        }

        private void MovieDetailsComponent_Load(object sender, EventArgs e)
        {
            
        }

        private void btnAddDetails_Click(object sender, EventArgs e)
        {
            try
            {
                MovieDetailsDataRow movieDetails = new MovieDetailsDataRow()
                {
                    ID = this.movieManagerID,
                    IMDBId = this.lblMovieID.Text,
                    IMDBUrl = this.lblIMDBUrl.Text,
                    Rating = Convert.ToSingle(this.lblMovieRating.Text.IsNullOrWhitespace()?"0":this.lblMovieRating.Text),
                    Releasedate = Convert.ToDateTime(this.lblReleaseDate.Text),
                    Genre = this.lblGenre.Text,
                    StoryLine = this.rtbStoryline.Text,
                    TagLine = this.lblTagLine.Text,
                    Title = this.lblMovieTitle.Text,
                    MovieImage = RetriveImage(this.pbMoviePoster.ImageLocation)
                };
                DBStatus status = MovieDB.AddMovieDetails(movieDetails);
                if (status.Equals(DBStatus.RowExists))
                    MessageBox.Show("Movie already exists.");
                else if (status.Equals(DBStatus.Success))
                    MessageBox.Show("Movie details sucesssfully added");
                this.btnAddDetails.Enabled = false;
            }
            catch
            {
                MessageBox.Show("Unknown Error ccured. ReInstall application.");
            }
        }

        private byte[] RetriveImage(string url)
        {
           
            byte[] b;//=new byte[500000];
            if (!string.IsNullOrWhiteSpace(url))
            {
                HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create(url);
                WebResponse myResp = myReq.GetResponse();

                Stream stream = myResp.GetResponseStream();
                int i;
                using (BinaryReader br = new BinaryReader(stream))
                {
                    i = (int)(myResp.ContentLength);
                    b = br.ReadBytes(i);
                    br.Close();
                }
                myResp.Close();
                return b;
            }
            return new byte[0];

        }

        public void SetImage(byte[] imgStream)
        {
            MemoryStream imgstr = new MemoryStream(imgStream);
            
            try
            {
                if (imgstr == null || imgStream.Count()==0)
                    this.pbMoviePoster.Image = null;
                else
                    this.pbMoviePoster.Image = Image.FromStream(imgstr);
            }
            catch { }
        }

        internal void DisableAddButton()
        {
            this.btnAddDetails.Enabled=false;
        }

        private void btnEditDetails_Click(object sender, EventArgs e)
        {
            this.editComponent.UpDateMovieDetails(movieDetails, movieManagerID);
        }
    }
}
